

/**
 * 变换坐标
 */

export default class Transform{

  /**
   * 
   * @param {Cartesian3}} cartesian3 笛卡尔转经纬度
   * @returns 
   */

  static Cartesian3ToWGS84(viewer ,cartesian3){
    var ellipsoid = viewer.scene.globe.ellipsoid
    var cartographic = ellipsoid.cartesianToCartographic(cartesian3)
    var lat = Cesium.Math.toDegrees(cartographic.latitude)
    var lng = Cesium.Math.toDegrees(cartographic.longitude)
    var alt = cartographic.height
    return [lng , lat , alt]
  }

  /**
   * 经纬度坐标转笛卡尔坐标
   * @param {*} degrees 
   * @returns 
   */

  static WGS84ToCartesian3(degrees){
    return new Cesium.Cartesian3.fromDegrees(degrees[0] , degrees[1] , degrees[2] || 0)
  }

  /**
   * 
   * @param {*} viewer 
   * @param {*} cartesian3Array 
   * @returns 
   */
  static Cartesian3ArrayToWGS84Array(viewer , cartesian3Array){
    let positions = []
    
    for (let index = 0; index < cartesian3Array.length; index++) {
      const cartesian3 = cartesian3Array[index];
      let position = this.Cartesian3ToWGS84(viewer , cartesian3)
      positions.push(position)
    }
    return positions
  }


  static WGS84ArrayToCartesian3Array(WGS84Array){
    let positions = []
    for (let index = 0; index < WGS84Array.length; index++) {
      const element = WGS84Array[index];
      positions.push(element[0])
      positions.push(element[1])
    }
    return new Cesium.Cartesian3.fromDegreesArray(positions)
  }

  /**
   * 屏幕坐标到笛卡尔坐标
   * @param {Cartesian2}} windowCoord 
   */

  static WindowCoordToCartesian3(viewer , windowCoord){
    return viewer.scene.globe.pick(viewer.camera.getPickRay(windowCoord) , viewer.scene)
  }

  /**
   * 屏幕坐标到经纬度坐标 
   * @param {Cartesian2}} windowCoord 
   */

  static WindowCoordToWGS84(viewer , windowCoord){
    let cartesian3 =  this.WindowCoordToCartesian3(viewer , windowCoord)
    if(!cartesian3){
      return null
    }
    return this.Cartesian3ToWGS84(viewer , cartesian3)
  }


  /**
   * 普通经纬度数组转 cesium数组
   * @example [ [117,28] , [119,38] ] ==> [117,28,119,38]
   * @param {*} degreesArray 
   * @returns 
   */

  static DegreesArrayToCartesianArray(degreesArray , height){
    let positions = []

    for (let index = 0; index < degreesArray.length; index++) {
      const element = degreesArray[index]
      
      positions.push(element[0])
      positions.push(element[1])

      if(height){
        positions.push(-height)
      }
    }
    return positions
  }

}